package util;

import com.alibaba.fastjson.JSONObject;

/** 
* @author SZQ 
* @util
* @version 创建时间：2016年3月7日 下午2:47:13 
* 类说明 
*/

import java.lang.reflect.Method;
import java.util.HashMap;

class InvokMap {
	public String pack;
	public String func;
	private static HashMap<String, InvokMap> router = null;

	private InvokMap(String p, String f) {
		this.pack = p;
		this.func = f;
	}

	public synchronized static HashMap<String, InvokMap> getInstance() {
		if (router == null) {
			router = new HashMap<String, InvokMap>();
			router.put("bootVideo", new InvokMap("handler.CMDHandler", "bootVideo"));
		}
		return router;
	}
}

public class RouterHandler {

	private static HashMap<String, InvokMap> router = InvokMap.getInstance();

	/**
	 * @Description: TODO(方法路由)
	 * @author SZQ
	 * @date 2016年1月18日 下午4:21:01
	 * @version V1.0
	 */
	public JSONObject execute(Object func, String jsonStr) throws Exception {

		String classname = router.get(func).pack;
		String funcname = router.get(func).func;

		int argnum = 1;
		@SuppressWarnings("rawtypes")
		Class[] argt = new Class[argnum];
		Object[] argv = new Object[argnum];
		for (int i = 0; i < argnum; i++) {
			argt[i] = String.class;
			argv[i] = jsonStr;
		}

		@SuppressWarnings("rawtypes")
		Class classType = Class.forName(classname);
		Object invoker = classType.newInstance();
		@SuppressWarnings("unchecked")
		Method method = classType.getMethod(funcname, argt);
		Object result = method.invoke(invoker, argv);
		return (JSONObject) result;
	}
}
	
	


